package com.cet.algorithm.哈希表;

import java.util.*;

/**
 * @program: algorithm
 * @description: 两数组的交集
 * @author: 陈恩涛
 * @create: 2022-03-14 22:46
 **/
public class LC349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        int length1 = nums1.length;
        int length2 = nums2.length;
        Map<Integer, Integer> map = new HashMap<>();
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < length1; i++) {
            map.put(nums1[i], 1);
        }
        for (int i = 0; i < length2; i++) {
            if (map.get(nums2[i]) != null) {
                set.add(nums2[i]);
            }
        }
        int[] result = new int[set.size()];
        int i = -1;
        for (Integer item : set) {
            result[++i] = item;
        }
        return result;
    }
}
